package com.d3works.apps.base.web;

import com.d3works.apps.base.db.FileSourceConnector;
import com.d3works.apps.base.db.repo.FileSourceConnectorRepository;
import com.d3works.apps.base.service.filesourceconnector.FileSourceConnectorService;
import com.d3works.apps.base.translation.ITranslation;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;

public class FileSourceConnectorController {

  public void testConnection(ActionRequest request, ActionResponse response) {

    try {
      FileSourceConnector fileSourceConnector =
          Beans.get(FileSourceConnectorRepository.class)
              .find(request.getContext().asType(FileSourceConnector.class).getId());
      if (fileSourceConnector != null) {

        if (Beans.get(FileSourceConnectorService.class).isValid(fileSourceConnector)) {
          response.setInfo(I18n.get(ITranslation.BASE_FILE_SOURCE_CONNECTOR_SUCCESS_CONNECTION));
          return;
        }
      }
      response.setInfo(I18n.get(ITranslation.BASE_FILE_SOURCE_CONNECTOR_FAILED_CONNECTION));
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
